package sz.szsmk.citizencard.mvp.dzsmkpresenter;

import com.google.gson.Gson;
import sz.szsmk.citizencard.model.network.dzbean.req.CommonRequest;
import sz.szsmk.citizencard.model.network.dzbean.res.Bank;
import sz.szsmk.citizencard.model.network.dzbean.res.SupportBankResponse;
import sz.szsmk.citizencard.mvp.BankListMvpView;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.HttpApi;
import sz.szsmk.citizencard.smkretrofit.NetworkDateSource;
import sz.szsmk.citizencard.util.ObjectSignatureUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import rx.functions.Action1;

/**
 * Created by yangzhengwen on 2017/10/1.
 */

public class BankPresenter extends AppBasePresenter<BankListMvpView> {

    @Inject
    NetworkDateSource mNetworkDateSource;
    private ArrayList<Bank> list = new ArrayList<>();

    @Inject
    public BankPresenter() {
        getComponent().inject(this);
    }

    public void getSupportBankList() {
        checkViewAttached();
        HttpApi httpApi = mNetworkDateSource.getHttpApi();
        CommonRequest commonRequest = ObjectSignatureUtil.getCommonRequest();
        mNetworkDateSource.getSupportBankList(commonRequest, new Action1<SupportBankResponse>() {

            private Bank bank;

            @Override
            public void call(SupportBankResponse baseRes) {
                String code = baseRes.getCode();
                if (code !=null && code.equals("0000")){
                    List<SupportBankResponse.DataBean> data = baseRes.getData();
                    if (data==null){
                        return;
                    }
                    for (int i = 0; i < data.size(); i++) {
                        SupportBankResponse.DataBean dataBean = data.get(i);
                        String json = new Gson().toJson(dataBean);
                        bank = new Gson().fromJson(json, Bank.class);
                        list.add(bank);
                    }
                    getMvpView().onGetBanksInfo(list);
                }else {
                    getMvpView().onGetBanksFail();
                }
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                String message = throwable.getMessage();
            }
        });

    }
}
